home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 125
/
Freelog_MarsAvril2015_No125.iso
/
Musique
/
Quod Libet
/
quodlibet-3.3.0-installer.exe
/
bin
/
quodlibet
/
mmkeys
/
osx.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2014-12-31
|
4KB
|
104 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
import threading
from gi.repository import GLib
from _base import MMKeysBackend, MMKeysAction, MMKeysImportError
try:
from AppKit import NSKeyUp, NSSystemDefined, NSEvent
import Quartz
except ImportError:
raise MMKeysImportError
class OSXBackend(MMKeysBackend):
def __init__(self, name, callback):
self._OSXBackend__eventsapp = MacKeyEventsTap(callback)
self._OSXBackend__eventsapp.start()
def cancel(self):
if self._OSXBackend__eventsapp is not None:
self._OSXBackend__eventsapp.stop()
self._OSXBackend__eventsapp = None
class MacKeyEventsTap(threading.Thread):
_EVENTS = {
16: MMKeysAction.PLAYPAUSE,
19: MMKeysAction.NEXT,
20: MMKeysAction.PREV }
def __init__(self, callback):
super(MacKeyEventsTap, self).__init__()
self._callback = callback
self._tap = None
self._runLoopSource = None
self._event = threading.Event()
def _push_callback(self, action):
def idle_call(action):
if self._tap:
self._callback(action)
return False
GLib.idle_add(idle_call, action)
def _event_tap(self, proxy, type_, event, refcon):
if type_ == Quartz.kCGEventTapDisabledByUserInput or type_ == Quartz.kCGEventTapDisabledByTimeout:
Quartz.CGEventTapEnable(self._tap, True)
return event
keyEvent = None.eventWithCGEvent_(event)
if keyEvent.subtype() is 8:
data = keyEvent.data1()
keyCode = (data & 0xFFFF0000L) >> 16
keyState = (data & 65280) >> 8
if keyCode in self._EVENTS:
if keyState == NSKeyUp:
self._push_callback(self._EVENTS[keyCode])
return None
return event
def _loop_start(self, observer, activiti, info):
self._event.set()
def run(self):
self._tap = Quartz.CGEventTapCreate(Quartz.kCGSessionEventTap, Quartz.kCGHeadInsertEventTap, Quartz.kCGEventTapOptionDefault, Quartz.CGEventMaskBit(NSSystemDefined), self._event_tap, None)
if self._tap is None:
self._event.set()
return None
self._loop = None.CFRunLoopGetCurrent()
self._observ = Quartz.CFRunLoopObserverCreate(None, Quartz.kCFRunLoopEntry, False, 0, self._loop_start, None)
Quartz.CFRunLoopAddObserver(self._loop, self._observ, Quartz.kCFRunLoopCommonModes)
self._runLoopSource = Quartz.CFMachPortCreateRunLoopSource(None, self._tap, 0)
Quartz.CFRunLoopAddSource(self._loop, self._runLoopSource, Quartz.kCFRunLoopDefaultMode)
Quartz.CGEventTapEnable(self._tap, True)
Quartz.CFRunLoopRun()
def stop(self):
'''Call once from the main thread to stop the thread.
After this returns no callback will be called anymore.
'''
self._event.wait()
if self._tap is None:
return None
None.CGEventTapEnable(self._tap, False)
self._tap = None
Quartz.CFRunLoopRemoveSource(self._loop, self._runLoopSource, Quartz.kCFRunLoopDefaultMode)
self._runLoopSource = None
Quartz.CFRunLoopRemoveObserver(self._loop, self._observ, Quartz.kCFRunLoopCommonModes)
self._observ = None
Quartz.CFRunLoopStop(self._loop)
self._loop = None